﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using Microsoft.SharePoint;
using DE.Sdc.SharePoint.SPContentStructure.Common;

namespace DE.Sdc.SharePoint.SPContentStructure.Import
{
    [Serializable]
    public class View : IXmlSerializable, IImportDefinition<SPView, SPList>
    {
        public XmlElement Xml { get; set; }

        [XmlIgnore]
        public ActionType Action
        {
            get
            {
                if (Xml.Attributes["Action"] != null && Xml.Attributes["Action"].Value != null)
                    return (ActionType)Enum.Parse(typeof(ActionType), Xml.Attributes["Action"].Value, true);
                
                return ActionType.AddOrUpdate;
            }
            set
            {
                if (Xml.Attributes["Action"] == null)
                {
                    Xml.SetAttribute("Action", value.ToString());
                }
                else
                {
                    Xml.Attributes["Action"].Value = value.ToString();
                }
            }
        }

        [XmlIgnore]
        public string Name
        {
            get
            {
                return Xml.Attributes["Name"].Value;
            }
        }

        [XmlIgnore]
        public string DisplayName
        {
            get
            {
                return Xml.Attributes["DisplayName"].Value;
            }
            set
            {
                Xml.Attributes["DisplayName"].Value = value;
            }
        }

        [XmlIgnore]
        public SPView UpdatedSPObject { get; internal set; }

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(reader.ReadOuterXml());
            Xml = xml.DocumentElement;
        }

        public void WriteXml(XmlWriter writer)
        {
            if (Xml != null)
            {
                for (int i = 0; i < Xml.Attributes.Count; i++)
                {
                    XmlAttribute attr = Xml.Attributes[i];
                    writer.WriteAttributeString(attr.Name, attr.Value);
                }

                for (int i = 0; i < Xml.ChildNodes.Count; i++)
                {
                    XmlNode node = Xml.ChildNodes[i];
                    writer.WriteNode(node.CreateNavigator(), true);
                }

                //writer.WriteNode(Xml.CreateNavigator(), true);
            }
        }

        public void ImportDefinition(SPView view, SPList parentList)
        {
            if (Action == ActionType.AddOrUpdate)
                throw new ContentStructureException(
                    string.Format("Please use 'Replace' Action for update the view '{0}'.", DisplayName));

            if ((Action == ActionType.Delete ||
                 Action == ActionType.Replace) && view != null)
            {
                parentList.Views.Delete(view.ID);
                view = null;
            }

            if (view == null &&
                (Action == ActionType.AddOrUpdateOrReplace))
            {
                XmlDocument viewDocument = new XmlDocument();
                viewDocument.LoadXml(DeploymentHelper.RemoveNamespace(Xml.OuterXml,
                                                                      Xml.NamespaceURI));
                view = new SPView(parentList, viewDocument);

                // The clone method creates the view in the view collection of the list.
                SPView clonedView = view.Clone(DisplayName, view.RowLimit, view.Paged, view.DefaultView);
                clonedView.Scope = view.Scope;
                clonedView.AggregationsStatus = view.AggregationsStatus;
                clonedView.Hidden = view.Hidden;
                clonedView.Update();
            }

            UpdatedSPObject = view;
        }

    }
}
